/*
 * @Author: sean.kei@163.com
 * @Date: 2021-08-27 10:31:05
 * @LastEditors: sean_kei@163.com
 * @LastEditTime: 2021-12-01 16:58:18
 */
package com.wxhandle.cleandemo.sso.config;

// import com.wxhandle.cleandemo.core.services.SysUserService;
import com.wxhandle.cleandemo.sso.service.UserDetailsServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@EnableGlobalMethodSecurity(
    prePostEnabled = true, 
    order = 0, 
    mode = AdviceMode.PROXY,
    proxyTargetClass = false
)
@EnableWebSecurity
public class DefaultSecurityConfig {

    @Autowired 
	private UserDetailsServiceImpl userService;
	@Bean
	SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
		http
			.authorizeRequests(authorizeRequests ->
				authorizeRequests.anyRequest().authenticated()
			)
			.formLogin(Customizer.withDefaults())
			.csrf()
			.and().headers().frameOptions().sameOrigin();
		return http.build();
	}

	@Autowired
	protected void configureGlobal(AuthenticationManagerBuilder builder) throws Exception {
		// LOGGER.debug("in configureGlobal");
		 builder
             .userDetailsService(this.userService)
			 .and()
			//  .authenticationProvider(authenticationProvider)
                // .passwordEncoder(passwordEncoder())
        //  .and()
             .eraseCredentials(true);
	}
	
	// @Bean
	// public UserDetailsService userDetailsService() {
	// 	return new UserDetailsServiceImpl(userService);
	// }
}
